package com.eedi.org.hospital.controller;

import com.eedi.framework.common.pojo.CommonResult;
import com.eedi.framework.common.pojo.PageResult;
import com.eedi.framework.common.util.object.BeanUtils;
import com.eedi.framework.common.validation.AddGroup;
import com.eedi.framework.common.validation.UpdateGroup;
import com.eedi.framework.hospdept.core.enums.HospStandardDeptTypeEnum;
import com.eedi.framework.hospdept.core.utils.HospDeptUtils;
import com.eedi.org.hospital.controller.vo.dept.OrgHospDeptPageReqVO;
import com.eedi.org.hospital.controller.vo.dept.OrgHospDeptRespVO;
import com.eedi.org.hospital.controller.vo.dept.OrgHospDeptSaveReqVO;
import com.eedi.org.hospital.controller.vo.dept.OrgHospitalDeptUpdateStatusReqVO;
import com.eedi.org.hospital.dal.dataobject.OrgHospDeptDO;
import com.eedi.org.hospital.service.OrgHospDeptService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.util.CollectionUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.annotation.security.PermitAll;
import javax.validation.Valid;

import static com.eedi.framework.common.pojo.CommonResult.success;


@Tag(name = "管理后台 - 医院科室")
@RestController
@RequestMapping("/hosp-dept")
@Validated
public class OrgHospDeptController {

    @Resource
    private OrgHospDeptService orgHospDeptService;

    @PostMapping("/create")
    @Operation(summary = "创建医院科室")
    // @PreAuthorize("@ss.hasPermission('org:hosp-dept:create')")
    public CommonResult<String> createHospDept(@Validated({AddGroup.class}) @RequestBody OrgHospDeptSaveReqVO createReqVO) {
        return success(orgHospDeptService.createHospDept(createReqVO));
    }

    @PostMapping("/update")
    @Operation(summary = "更新医院科室")
    // @PreAuthorize("@ss.hasPermission('org:hosp-dept:update')")
    public CommonResult<Boolean> updateHospDept(@Validated({UpdateGroup.class}) @RequestBody OrgHospDeptSaveReqVO updateReqVO) {
        orgHospDeptService.updateHospDept(updateReqVO);
        return success(true);
    }


    @PostMapping("/update-status")
    @Operation(summary = "修改医院科室状态")
    //   @PreAuthorize("@ss.hasPermission('org:hosp-dept:updateStatus')")
    @PermitAll
    public CommonResult<Boolean> updateHospDeptStatus(@Valid @RequestBody OrgHospitalDeptUpdateStatusReqVO reqVO) {
        orgHospDeptService.updateHospDeptStatus(reqVO.getOrgHospDeptId(), reqVO.getOrgStatus());
        return success(true);
    }

    @GetMapping("/delete")
    @Operation(summary = "删除医院科室")
    @Parameter(name = "orgHospDeptId", description = "编号", required = true)
    // @PreAuthorize("@ss.hasPermission('org:hosp-dept:delete')")
    public CommonResult<Boolean> deleteHospDept(String orgHospDeptId) {
        orgHospDeptService.deleteHospDept(orgHospDeptId);
        return success(true);
    }

    @PostMapping("/page")
    // @PreAuthorize("@ss.hasPermission('org:hosp-dept:query')")
    public CommonResult<PageResult<OrgHospDeptRespVO>> getHospDeptPage(@RequestBody @Valid OrgHospDeptPageReqVO pageReqVO) {
        PageResult<OrgHospDeptDO> pageResult = orgHospDeptService.getHospDeptPage(pageReqVO);
        PageResult<OrgHospDeptRespVO> hospDeptRespVOPageResult = BeanUtils.toBean(pageResult, OrgHospDeptRespVO.class);
        if (CollectionUtils.isEmpty(hospDeptRespVOPageResult.getList())) {
            return success(PageResult.empty());
        }
        //返回一级科室页面，方便编辑页面回显
        hospDeptRespVOPageResult.getList().forEach(x -> {
            x.setOrgHospStandardDeptFirstCode(String.valueOf(HospDeptUtils.getParentIdByType(Integer.valueOf(x.getOrgHospStandardDeptCode()), HospStandardDeptTypeEnum.SECONE_LEVEL)));
        });
        return success(hospDeptRespVOPageResult);
    }

}
